package com.css.fxfzdpzs.modules.fwjg.repository;

import com.css.fxfzdpzs.modules.fwjg.entity.first.*;
import com.css.fxfzdpzs.modules.fwjg.entity.second.Fwjg5yearsSubmitSituation;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgProjectSituation;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgProjectType;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgSubmitNum;
import com.css.fxfzdpzs.modules.fwjg.entity.third.FwjgViewDetails;
import com.css.fxfzdpzs.modules.fwjg.vo.FwjgViewDetailsReport;
import com.css.fxfzdpzs.util.PlatformObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author y1fan
 * @description
 */
@Repository
public class FwjgRepositoryImpl implements FwjgRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public List<FwjgProHouseNumCount> getHouseNumCount() {
        String sql = "select * from fwjg_pro_house_num_count ";
        List<FwjgProHouseNumCount> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgProHouseNumCount.class));
        return list;
    }

    @Override
    public List<FwjgInformationSystem> getInformationSystem() {
        String sql = "select * from fwjg_information_system ";
        List<FwjgInformationSystem> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgInformationSystem.class));
        return list;
    }

    @Override
    public FwjgProcessSituation getProcessSituation() {
        String sql = "select * from fwjg_process_situation ";
        List<FwjgProcessSituation> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgProcessSituation.class));
        if (PlatformObjectUtils.isEmpty(list)){
            return null;
        }else {
            return list.get(0);
        }
    }

    @Override
    public List<Fwjg5yearsSituation> get5yearsSituation() {
        String sql = "select * from fwjg_5years_situation  order by  years desc  limit 5 offset 0";
        List<Fwjg5yearsSituation> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Fwjg5yearsSituation.class));
        return list;
}

    @Override
    public List<FwjgProvinceNum> getProvinceNum() {
        String sql = "select * from fwjg_province_num order by num desc ";
        List<FwjgProvinceNum> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgProvinceNum.class));
        return list;
    }

    @Override
    public List<FwjgTypicalCase> fwjgRepository() {
        String sql = "select * from fwjg_typical_case ";
        List<FwjgTypicalCase> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgTypicalCase.class));
        return list;
    }

    @Override
    public List<FwjgProjectSituation> getProjectSituation(String flag) {
        String sql = "select * from fwjg_project_situation     where flag ='"+flag+"'  order by num  desc limit 5 offset 0 ";
        List<FwjgProjectSituation> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgProjectSituation.class));
        return list;
    }

    @Override
    public List<FwjgProjectType> getProjectType(String flag) {
        String sql = "select * from fwjg_project_type  where flag='"+flag+"'  order by num   ";
        List<FwjgProjectType> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgProjectType.class));
        return list;
    }

    @Override
    public List<Fwjg5yearsSubmitSituation> get5yearsSubmitSituation() {
        String sql = "select * from fwjg_5years_submit_situation  order by year desc limit 5 offset 0   ";
        List<Fwjg5yearsSubmitSituation> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Fwjg5yearsSubmitSituation.class));
        return list;
    }

    @Override
    public FwjgViewDetails getViewDetails(String id) {
        String sql = "select * from fwjg_typical_case  where id='"+id+"'   ";
        List<FwjgViewDetails> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgViewDetails.class));
        if (PlatformObjectUtils.isEmpty(list)){
            return null;
        }else {
            return list.get(0);
        }
    }

    @Override
    public List<FwjgViewDetailsReport> getReport(String tcId) {
        String sql = "select * from fwjg_view_details_report  where tc_id='"+tcId+"'   ";
        List<FwjgViewDetailsReport> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgViewDetailsReport.class));
        return list;
    }

    @Override
    public List<FwjgSubmitNum> getSubmitNum() {
        String sql = "select * from fwjg_submit_num order by num desc limit 5 offset 0    ";
        List<FwjgSubmitNum> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgSubmitNum.class));
        return list;
    }

    @Override
    public List<FwjgTypicalCase> getMapViewCase(String type, String category,String provinceCode) {
        StringBuilder sql = new StringBuilder(" select id,name,type,category from fwjg_typical_case  ");
        StringBuilder whereSql=new StringBuilder(" where 1=1 ");
        if(StringUtils.isNotBlank(type)){
            whereSql.append(" and type='"+type+"'");
        }
        if(StringUtils.isNotBlank(category)){
            whereSql.append(" and category='"+category+"'");
        }
        if(StringUtils.isNotBlank(provinceCode)){
            whereSql.append(" and province_code='"+provinceCode+"'");
        }
        List<FwjgTypicalCase> list = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(FwjgTypicalCase.class));
        return list;
    }

    @Override
    public List<FwjgMapTypicalCase> getDisprovince(String type) {
        StringBuilder sql = new StringBuilder();
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        sql.append("select * from  fwjg_map_date");
        if(StringUtils.isNotBlank(type)){
            whereSql.append(" and type='"+type+"'");
        }
        List<FwjgMapTypicalCase> list = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(FwjgMapTypicalCase.class));
        return list;
    }

    @Override
    public List<FwjgMapTypicalCase> getSubmitTopNum() {
        String sql = "select location,count(1) as num from fwjg_typical_case GROUP BY location order by num desc limit 5 ";
        List<FwjgMapTypicalCase> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwjgMapTypicalCase.class));
        return list;
    }
}
